package com.gingko.duominuo.utils

import android.content.Intent
import android.net.Uri
import android.os.Environment
import com.gingko.duominuo.App
import com.gingko.duominuo.constants.*
import java.io.File

/**
 * 获取应用程序缓存根文件
 *
 * @return
 */
val appCache: File
    get() {
        val cacheFile: File = if (isSDCardEnable()) {
            File(Environment.getExternalStorageDirectory(), "TTSH")
        } else {
            File(Environment.getDataDirectory(), "TTSH")
        }
        createOrExistsDir(cacheFile.absolutePath)
        return cacheFile
    }


/**
 * 图片临时处理目录
 *
 * @return
 */
val imageCache: File
    get() {
        val imageCache = File(appCache, FILE_IMAGE_CACHE)
        createOrExistsDir(imageCache.absolutePath)
        return imageCache
    }

/**
 * 更新相册
 *
 * @param file
 */
fun sendUpdateBroadcast(file: File?) {
    val intent = Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE)
    val uri = Uri.fromFile(file)
    intent.data = uri
    App.instance.sendBroadcast(intent)
}

fun clearCacheFile() {
    deleteDir(appCache.absolutePath)
}

fun isSDCardEnable(): Boolean {
    return Environment.MEDIA_MOUNTED == Environment.getExternalStorageState()
}